package com.foreknow.dao.impl;

import com.foreknow.dao.AdminDao;

import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;
import com.foreknow.model.Admin;

import java.sql.SQLException;
import java.util.List;

public class AdminDaoImpl extends BaseDao implements AdminDao {
    @Override
    public boolean save(Admin admin) throws SQLException{
        String sql = "insert into admin(name,pass) values(?,?)";
        int isRight = jt.update(sql,admin.getName(),admin.getPass());
        return isRight == 1;
    }

    @Override
    public boolean deleteById(int id) throws SQLException{
        String sql = "delete from admin where id=?";
        int isRight = jt.update(sql,id);
        return isRight == 1;
    }

    @Override
    public Admin getQueryById(int id) {
        String sql = "select * from admin where id=?";
        EntityMapping mapping = mappingFactory.getMapping(MappingFactory.ADMIN_MAPPING);
        List<Object> list = jt.query(sql,mapping,id);
        if(list.size() > 0) {
            return (Admin) list.get(0);
        }else {
            return null;
        }
    }

    @Override
    public boolean update(Admin admin)throws SQLException {
        String sql = "update admin set name=?,pass=? where id=?";
        int isRight = jt.update(sql,admin.getName(),admin.getPass(),admin.getId());
        return isRight == 1;
    }

    @Override
    public List<Object> getQueryAll() {
        String sql = "select * from admin";
        EntityMapping mapping = mappingFactory.getMapping(MappingFactory.ADMIN_MAPPING);
        List<Object> list = jt.query(sql,mapping);
        return list;
    }
}

